﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KnowlegeAssistant
{
    public partial class StatisticsForm : Form
    {
        private int user_id = 0;
        private int group_id = 0;
        private int archive_test_id = 0;
        private DataTable table;
        private User user;

        public StatisticsForm(int user_id, int group_id, int archive_test_id, User user)
        {
            InitializeComponent();
            this.user_id = user_id;
            this.group_id = group_id;
            this.archive_test_id = archive_test_id;
            this.user = user;

            if (group_id == 0)
            {
                group_print_button.Visible = false;
            }
            else { group_print_button.Visible = true; }

            if (user.OwnerType == 1)
            {
                button3.Visible = false;
                button3.Enabled = false;
            }

            RefreshStatisticsTableData();
        }

        private void RefreshStatisticsTableData()
        {
            List<string> parameters = new List<string>() { };
            parameters.Add(user_id.ToString());
            parameters.Add(group_id.ToString());
            parameters.Add(archive_test_id.ToString());

            dataGridView1.AutoGenerateColumns = false;
            this.table = DatabaseConnect.execSP("GetTestResults", parameters).Tables[0];
            dataGridView1.DataSource = this.table;
            InitTableFields();
        }

        private void InitTableFields()
        {
            dataGridView1.Columns[0].DataPropertyName = "fio";
            dataGridView1.Columns[1].DataPropertyName = "group_number";
            dataGridView1.Columns[2].DataPropertyName = "subject_name";
            dataGridView1.Columns[3].DataPropertyName = "theme_name";
            dataGridView1.Columns[4].DataPropertyName = "countoftrue";
            dataGridView1.Columns[5].DataPropertyName = "countoffalse";
            dataGridView1.Columns[6].DataPropertyName = "passed_points";
            dataGridView1.Columns[7].DataPropertyName = "max_points";
            dataGridView1.Columns[8].DataPropertyName = "percents";
            dataGridView1.Columns[9].DataPropertyName = "mark";
            dataGridView1.Columns[10].DataPropertyName = "duration_time";
            dataGridView1.Columns[11].DataPropertyName = "date_begin";
        }

        private void StatisticsForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            int archive_test = (int)table.Rows[dataGridView1.SelectedRows[0].Index]["arc_test_id"];
            PrintPreviewForm ppf = new PrintPreviewForm(archive_test);
            ppf.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int archive_test = (int)table.Rows[dataGridView1.SelectedRows[0].Index]["arc_test_id"];
            TestResultsForm trf = new TestResultsForm(archive_test);
            trf.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes ==
                MessageBox.Show("Вы дейтвительно хотите удалить выделенные результаты?", "Удаление результатов", MessageBoxButtons.YesNo))
            {
                foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                {
                    TestUtil.DeleteArchiveTest((int)table.Rows[row.Index]["arc_test_id"]);
                }
                RefreshStatisticsTableData();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PrintPreviewForm ppf = new PrintPreviewForm(0,this.group_id);
            ppf.ShowDialog();
        }
    }
}
